package Servlets;

import java.io.IOException;
import java.util.Hashtable;
import java.util.List;

import javax.jms.JMSException;
import javax.jms.Queue;
import javax.jms.QueueConnection;
import javax.jms.QueueConnectionFactory;
import javax.jms.QueueSender;
import javax.jms.QueueSession;
import javax.jms.Session;
import javax.jms.TextMessage;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import BusinessDelegate.FacadeBusinessDelegate;
import EntitiesDTO.ItemSolicitudCotizacionDTO;
import EntitiesDTO.SolicitudCompraDTO;
import Parser.WriterXML;

/**
 * Servlet implementation class for Servlet: ProcesarSolicitudCompra
 * 
 */
public class ProcesarSolicitudCompra extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet {
	static final long serialVersionUID = 1L;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public ProcesarSolicitudCompra() {
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	@SuppressWarnings("unchecked")
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		HttpSession session = request.getSession(true);
		try{
		List<ItemSolicitudCotizacionDTO> itemCompra = (List<ItemSolicitudCotizacionDTO>) session
				.getAttribute("itemsCompra");
		
		
		String mensaje = WriterXML.getInstance().SolicitarCompra(itemCompra);

		Hashtable props = new Hashtable();
		props.put(InitialContext.INITIAL_CONTEXT_FACTORY,
				"org.jnp.interfaces.NamingContextFactory");
		props.put(InitialContext.PROVIDER_URL, "jnp://5.159.16.73:1099");

		InitialContext ctx;
		try {
			ctx = new InitialContext(props);
			// buscar la Connection Factory en JNDI
			QueueConnectionFactory qfactory = (QueueConnectionFactory) ctx
					.lookup("ConnectionFactory");
			// buscar la Cola en JNDI
			Queue queue = (Queue) ctx.lookup("queue/colaSolCom");
			// crear la connection y la session a partir de la connection
			QueueConnection qCon = qfactory.createQueueConnection();
			QueueSession qSession = qCon.createQueueSession(false,
					Session.AUTO_ACKNOWLEDGE);
			// crear un producer para enviar mensajes usando la session
			QueueSender qSender = qSession.createSender(queue);
			// crear un mensaje de tipo text y setearle el contenido
			TextMessage message = qSession.createTextMessage();
			message.setText(mensaje);
			// enviar el mensaje
			qSender.send(message);

		} catch (NamingException e) {
			e.printStackTrace();
		} catch (JMSException e) {
			e.printStackTrace();
		}

			
		
		
		
		
		SolicitudCompraDTO soli = new SolicitudCompraDTO();
		soli.setItemsSolicitudCotizacion(itemCompra);
		
		FacadeBusinessDelegate.getInstance().setNuevaSolicitudCompra(soli);
		
		
		
		session.setAttribute("itemsCompra", itemCompra);
		RequestDispatcher dispatcher = request
		.getRequestDispatcher("./MostrarCompra.jsp");
		try {
			dispatcher.forward(request, response);
		} catch (ServletException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		}catch (NullPointerException e){
			System.out.println("Se desea procesar una compra sin items - ERROR");
		}catch (NamingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}


		
	}
}